package com.cxh.rmsq.ui.activity.self

import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.cxh.rmsq.R
import com.cxh.rmsq.databinding.ActivityFansBinding
import com.cxh.rmsq.im.IMManager
import com.cxh.rmsq.net.newNet.ProjectViewModel
import com.cxh.rmsq.ui.activity.TitleBaseActivity
import com.cxh.rmsq.ui.adapter.self.FansListAdapter
import com.cxh.rmsq.utils.ToastUtils
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

/**
 * 订阅、粉丝页面
 */
class FansActivity : TitleBaseActivity() {
    private lateinit var binding: ActivityFansBinding
    private var type = 0 //1订阅2粉丝
    private var listAdapter: FansListAdapter? = null
    private var page = 1
    private val viewModel by lazy {
        ViewModelProvider.NewInstanceFactory().create(ProjectViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityFansBinding.inflate(layoutInflater)
        setContentView(binding.root)

        var isFans = intent.getBooleanExtra("isFans", false) as Boolean
        if (isFans) {
            type = 2
            titleBar.tvTitle.text = "粉丝"
        } else {
            type = 1
            titleBar.tvTitle.text = "订阅"
        }
        initView()
    }

    fun initView() {
        binding.rvList.setLayoutManager(LinearLayoutManager(this))
        listAdapter = FansListAdapter(type)
        listAdapter!!.setEmptyView(View.inflate(this, R.layout.empty_data_layout, null))

        binding.rvList.setAdapter(listAdapter)

        listAdapter!!.setOnItemChildClickListener(OnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.stv_focus -> {
                    val paramsMap = HashMap<String, Any>()
                    paramsMap["followerUserId"] = listAdapter!!.data[position].followerUserId
                    paramsMap["userId"] = IMManager.getInstance().currentId
                    paramsMap["isFollower"] = 0 //1:订阅 0：取消订阅
                    viewModel.createFollower(paramsMap) {
                        if (it.code == 200) {
                            ToastUtils.showToast("取消成功")
                            onResume()
                        } else {
                            ToastUtils.showToast(it.msg)
                        }
                    }
                }
            }
        })

        binding.refreshList.resetNoMoreData()
        binding.refreshList.setRefreshHeader(MaterialHeader(this))
        binding.refreshList.setRefreshFooter(ClassicsFooter(this))
        binding.refreshList.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                getData()
                refreshLayout.finishLoadMore()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                listAdapter!!.setNewInstance(null)
                getData()
            }
        })
    }

    override fun onResume() {
        super.onResume()
        page = 1
        listAdapter!!.setNewInstance(null)
        getData()
    }

    private fun getData() {
        viewModel.getSaveFollowerPage(type, page, 10) {
    //                Log.e("total", result.data.getTotal() + "");
            if (binding.refreshList.isRefreshing()) {
                binding.refreshList.finishRefresh()
            }
            if (it.data != null) {
                if (page == 1) {
                    listAdapter!!.setNewInstance(it.data.list)
                    if (it.data.list!!.size < 10) {
                        binding.refreshList.setEnableLoadMore(false)
                    } else {
                        binding.refreshList.setEnableLoadMore(true)
                    }
                } else {
                    if (it.data.list!!.size > 0) {
                        listAdapter!!.addData(it.data.list!!)
                        binding.refreshList.setEnableLoadMore(true)
                    } else {
                        binding.refreshList.setEnableLoadMore(false)
                        //                            listAdapter.getLoadMoreModule().loadMoreEnd(false);
    //                            listAdapter.getLoadMoreModule().loadMoreComplete();
                        binding.refreshList.finishLoadMoreWithNoMoreData()
                    }
                }
                listAdapter!!.notifyDataSetChanged()
            }
        }
    }
}